/* @(#)GSDataOperImpAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   vincent         2006-2-21
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.lib.db.backup.DataOper;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSDataOperImpAction extends GSBaseAction{

    public String execute(GSBaseForm aForm) throws Exception {
    	String tFileName=aForm.getRequest().getParameter("filename");
    	try {
	        File tFile = new File(tFileName);
	        FileInputStream tFis;
	        tFis = new FileInputStream(tFile);
	        InputStreamReader tReader = new InputStreamReader(tFis);
	        BufferedReader tIn     = new BufferedReader(tReader);
	        String strTemp= tIn.readLine();
	    	if(strTemp==null){
	    		aForm.getRequest().setAttribute("exception_code",GSException.E500201);
	    		aForm.getRequest().setAttribute("exception_desc",GSException.E500201_MSG);
	    		aForm.getRequest().setAttribute("action","A500201.do");
	    		return GSBaseConstants.FORWARD_ERROR;
	    	}
           }catch (FileNotFoundException e) {
	    		aForm.getRequest().setAttribute("exception_code",GSException.E500203);
	    		aForm.getRequest().setAttribute("exception_desc",GSException.E500203_MSG);
	    		aForm.getRequest().setAttribute("action","A500201.do");
	    		return GSBaseConstants.FORWARD_ERROR;

           }

	    	
	    String tTemp=aForm.getRequest().getParameter("tables");
     	
		StringBuffer tImpTemp = new StringBuffer();
//		tExpTemp = "exp system/sa@BUS file=d:/xxxx.dmp tables=(scott.EMP,scott.BONUS)";
		tImpTemp.append("imp ");
		tImpTemp.append(GSSystemConfigurator.getParam("gs.connectdatabase"));
		tImpTemp.append(" file="+tFileName);
		tImpTemp.append(" ignore=y");
		tImpTemp.append(" grants=y");
		if("all".equals(tTemp.toLowerCase())||"".equals(tTemp)||tTemp==null)
			//tExpTemp.append(" full=y");
			tImpTemp.append(" full=y");
		else
			tImpTemp.append(" tables=("+tTemp+")");
		int tResult=-1;
		GSLogWriter.debug("tExpTemp="+tImpTemp.toString());
		try {
			//0 成功 1 备份失败
			tResult=new DataOper().dataFunction(tImpTemp.toString());
			GSLogWriter.info(DateUtil.getCurrentTime("DT", "AD")+"==恢复 data tResult="+tResult);
		} catch (Exception ex) {
			GSLogWriter.error(this,ex.getMessage());
		}
    	if(tResult==0){
    		aForm.getRequest().setAttribute("message","文件成功恢复");
    		aForm.getRequest().setAttribute("action","A500201.do");
    		return GSBaseConstants.FORWARD_SUCCESS;
    	}else{
    		aForm.getRequest().setAttribute("exception_code",GSException.E500202);
    		aForm.getRequest().setAttribute("exception_desc",GSException.E500202_MSG);
    		aForm.getRequest().setAttribute("action","A500201.do");
    		return GSBaseConstants.FORWARD_ERROR;
    	}
    }
}
